package fina2.dcs.fi;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import javax.naming.InitialContext;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.Synchronized;
import org.jboss.seam.log.Log;
import org.jboss.seam.log.Logging;

import fina2.dcs.common.UIComponentHelper;
import fina2.dcs.context.ContextHelper;
import fina2.dcs.fis.BankGroup;
import fina2.dcs.fis.BankGroupLocal;
import fina2.dcs.fis.BankType;
import fina2.dcs.fis.BankTypeLocal;
import fina2.dcs.system.SysStringLocal;

@Name("fiBankAddData")
@Scope(ScopeType.SESSION)
@Synchronized(timeout = 5000)
public class FiListBankAddData implements Serializable{
	private List<SelectItem> bankGroups = null;
	private String selectedGroup = null;
	private String code;
	private String shortName;
	private String swiftCode;
	private String name;
	private String address;
	private String phone;
	private String fax;
	private String email;
	private String telEx;
	private List<SelectItem> fiTypes = new ArrayList<SelectItem>();
	private String selectedFiType = null;

	private InitialContext ic = null;
	private BankTypeLocal fiTypeLocal = null;
	private BankGroupLocal bgLocal = null;
	private SysStringLocal sysLocal = null;

	private Log log = Logging.getLog(getClass());

	public FiListBankAddData() {
		try {
			ic = ContextHelper.getInitialContext();
			fiTypeLocal = ContextHelper.injectBankTypeLocal(ic);
			bgLocal = ContextHelper.injectBankGroupLocal(ic);
			sysLocal = ContextHelper.injectSysStringLocal(ic);
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
		}
	}

	public List<SelectItem> getBankGroups() {
		bankGroups = new ArrayList<SelectItem>();
		try {
			List<BankGroup> bgDefault = bgLocal.getDefaultBankGroups();
			for (int i = 0; i < bgDefault.size(); i++) {
				bankGroups.add(new SelectItem(bgDefault.get(i)));
			}
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
		}
		return bankGroups;
	}

	public void setBankGroups(List<SelectItem> bankGroups) {
		this.bankGroups = bankGroups;
	}

	public String getSelectedGroup() {
		return selectedGroup;
	}

	public void setSelectedGroup(String selectedGroup) {
		this.selectedGroup = selectedGroup;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getShortName() {
		return shortName;
	}

	public void setShortName(String shortName) {
		this.shortName = shortName;
	}

	public String getSwiftCode() {
		return swiftCode;
	}

	public void setSwiftCode(String swiftCode) {
		this.swiftCode = swiftCode;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public String getFax() {
		return fax;
	}

	public void setFax(String fax) {
		this.fax = fax;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getTelEx() {
		return telEx;
	}

	public void setTelEx(String telEx) {
		this.telEx = telEx;
	}

	public List<SelectItem> getFiTypes() {
		fiTypes = new ArrayList<SelectItem>();
		try {
			fiTypeLocal.init();
			List<BankType> fis = fiTypeLocal.getBankTypes();
			for (int i = 0; i < fis.size(); i++) {
				fiTypes.add(new SelectItem(fis.get(i)));
			}
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
		}
		return fiTypes;
	}

	public void setFiTypes(List<SelectItem> fiTypes) {
		this.fiTypes = fiTypes;
	}

	public String getSelectedFiType() {
		return selectedFiType;
	}

	public void setSelectedFiType(String selectedFiType) {
		this.selectedFiType = selectedFiType;
	}

	public void flushPanel() {
		UIComponentHelper.clearComponent(FacesContext.getCurrentInstance().getViewRoot());
		address = null;
		code = null;
		email = null;
		fax = null;
		name = null;
		phone = null;
		shortName = null;
		swiftCode = null;
		telEx=null;
	}

}
